using score_m.dbc;
using System;
using System.Data;

namespace score_m
{
    public partial class student_password : System.Web.UI.Page
    {
        private DatabaseConnect dbc = new DatabaseConnect();
        private void log(string str)
        {
            System.Diagnostics.Debug.WriteLine(str);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["student"] == null)
                    Response.Redirect("login.aspx");
            }
        }

        protected void submitBt_Click(object sender, EventArgs e)
        {
            string sid = Session["student"] as string;
            string oldPassword = oldPasswordInput.Text;
            string newPassword = newPasswordInput.Text;
            string confirmPassword = confirmPasswordInput.Text;

            if (string.IsNullOrEmpty(oldPassword) || string.IsNullOrEmpty(newPassword) || string.IsNullOrEmpty(confirmPassword))
            {
                errorMsg.Text = "请填写完整信息！";
                errorMsg.Visible = true;
                return;
            }

            if (newPassword != confirmPassword)
            {
                errorMsg.Text = "两次输入的新密码不一致！";
                errorMsg.Visible = true;
                return;
            }

            DataTable res = dbc.excList($"SELECT * FROM student WHERE sid='{sid}' AND password='{oldPassword}'");
            if (res.Rows.Count == 0)
            {
                errorMsg.Text = "原密码错误！";
                errorMsg.Visible = true;
                return;
            }

            try
            {
                dbc.exc($"UPDATE student SET password='{newPassword}' WHERE sid='{sid}'");
                errorMsg.Text = "密码修改成功！";
                errorMsg.ForeColor = System.Drawing.Color.Green;
                errorMsg.Visible = true;
                oldPasswordInput.Text = "";
                newPasswordInput.Text = "";
                confirmPasswordInput.Text = "";
            }
            catch (Exception ex)
            {
                log(ex.Message);
                errorMsg.Text = "密码修改失败，请重试！";
                errorMsg.Visible = true;
            }
        }
    }
} 